<?php

namespace app\admin\controller;

class Sales extends Common {
        /*
         * 银行管理
         */

        public function pay() {
                $map = $this->_search("PayCate");
                $this->_list(M("PayCate"), $map);
                return $this->fetch();
        }

        /*
         * 银行管理添加
         */

        public function pay_cate_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("PayCate", false, false);
                        } else {
                                $this->_insert("PayCate", false, false);
                        }
                } else {
                        if ($id) {
                                $nc = M("PayCate")->field(true)->find($id);
                                $this->assign('pay', $nc);
                        } else {
                                $nc['status'] = 0;
                                $this->assign('pay', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 银行管理删除
         */

        public function pay_cate_del($id = '') {
                $this->_delete("PayCate", $id);
        }

        /*
         * 订单管理
         */

        public function tours_order() {
                $map = $this->_search("ToursOrder");
                $map['is_del'] = '0';
                $start_date = I("date_start");
                $end_date = I("date_end");
                if ($start_date && $end_date) {
                        $start_date_time = strtotime($start_date);
                        $end_date_time = strtotime($end_date);
                        $map['create_time'] = array('between', array($start_date_time, $end_date_time));
                }

                //出发日期
                $go_date_start = I("go_date_start");
                $go_date_end = I("go_date_end");
                if (!empty($go_date_start) && !empty($go_date_end)) {
                        $map['go_date'] = array('between', array($go_date_start, $go_date_end));
                }
                $mid = get_member_id();
                $this->assign('mid', $mid);
                $this->_list(D("ToursOrder"), $map, 'order_id', false, true);
                return $this->fetch();
        }

        public function order_detail() {
                $id = I("get.orderid");
                $model = D("ToursOrder");
                $order = $model->relation(true)->where(array('order_id' => $id))->find();
                if ($order != false) {
                        //IP定位
                        $ip = new \org\iplocation\IpLocation('UTFWry.dat');
                        $location = $ip->getlocation();
                        $order['country'] = $location['country'];
                        $order['area'] = $location['area'];
                        //订单数统计
                        $order['count'] = $model->where(array('uid' => $order['uid']))->count();
                        $this->assign('order', $order);
                        return $this->fetch();
                } else {
                        $this->iError("数据不存在！");
                }
        }

        public function order_accept() {
                $uid = get_member_id();
                $id = I('get.orderid');
                $order = M("ToursOrder")->where(array('order_id' => $id))->find();
                if ($order['status'] > 0) {
                        $this->iError("已有人受理！");
                } else {
                        //接收支付金额
                        $payment_data['pay_status'] = 0;
                        $payment_data['order_id'] = $order['order_id'];
                        $payment_data['payment'] =($order['adult'] * $order['adult_price']) + ($order['child'] * $order['child_price']);
                        $ret = M("ToursPayment")->add($payment_data);
                        if ($ret == false) {
                                $this->iError("受理失败！");
                        }
                        $data = array('mid' => $uid, 'status' => 1);
                        M("ToursOrder")->where(array('order_id' => $id))->setField($data);
                        $this->sysAdd("受理了订单,订单ID=" . $id);
                        $this->delSuccess("成功受理！");
                }
        }

        public function order_add() {
                if (IS_POST) {
                        $id = I("post.order_id");
                        $payment = I("post.payment");
                        //编辑订单信息
                        $tours_order = M("ToursOrder");
                        if ($tours_order->create()) {
                                if ($id) {
                                        $tours_order->save();
                                        //修改成交金额
                                        M("ToursPayment")->where(array('order_id' => $id))->setField('payment', $payment);
                                        $this->sysAdd("编辑了订单,订单ID=" . $id);
                                        $this->iSucess("编辑成功，与客户意向达成一致即可确认订单以进入支付状态！");
                                } else {
                                        $this->iError("订单不存在！");
                                }
                        }
                } else {
                        $id = I("get.orderid");
                        if ($id) {
                                $model = D("ToursOrder");
                                $order = $model->relation(true)->where(array('order_id' => $id))->find();
                                $order['payment'] = M("ToursPayment")->where(array('order_id' => $id))->getField("payment");
                                $this->assign('order', $order);
                                return $this->fetch();
                        }
                }
        }

        public function order_affirm() {
                $id = I('get.orderid');
                $uid = get_member_id();
                M('ToursOrder')->where(array('order_id' => $id, 'mid' => $uid))->setField('status', 2);
                $this->sysAdd("确认了订单,订单ID=" . $id);
                $this->delSuccess("成功确认！");
        }

        public function order_handover() {
                if (IS_POST) {
                        $tid = I('post.tid');
                        if ($tid) {
                                $id = I('post.orderid');
                                $uid = get_member_id();
                                M("ToursOrder")->where(array('order_id' => $id, 'mid' => $uid))->setField("mid", $tid);
                                $this->sysAdd("移交了订单,订单ID=" . $id);
                                $this->iSucess('提交成功');
                        }
                } else {

                        //部门
                        $department = D("Department")->field("id,name")->where(array('status' => 1))->select();
                        foreach ($department as $key => $value) {
                                $department[$key]['member'] = D("Member")->field("id,realname,dept_id")->where(array('dept_id' => $value['id']))->select();
                        }
                        $id = I('get.orderid');
                        $html = '<input type="hidden" name="orderid" value="' . $id . '">';
                        $html .=' <input data-rule="required" type="hidden" name="tid" id="j_ztree_menus_membersingle" data-tree="j_select_tree_membersingle"  size="18" readonly >';
                        $html .= '<ul id="j_select_tree_membersingle" data-check-enable="true" data-chk-style="radio" data-radio-type="all" class="ztree" data-toggle="ztree" data-expand-all="false" data-on-check="S_NodeCheck" data-on-click="S_NodeClick">';
                        foreach ($department as $key => $value) {
                                $html .='<li data-id="' . $value['id'] . '" data-pid="0">' . $value['name'] . '</li>';
                                foreach ($value['member'] as $val) {
                                        $html .='<li data-id="yuan-' . $val['id'] . '" data-pid="' . $val['dept_id'] . '">' . $val['realname'] . '</li>';
                                }
                        }

                        $html .='</ul>';
                        $this->assign('html', $html);
                        return $this->fetch();
                }
        }

        public function order_complete() {
                $id = I('get.orderid');
                $uid = get_member_id();
                M('ToursOrder')->where(array('order_id' => $id, 'mid' => $uid))->setField('status', 4);

                $tourpayment = M('ToursPayment')->where(array('order_id' => $id))->find();
                if (empty($tourpayment['payment_time'])) {
                        $pay_data = array();
                        $pay_data['payment_time'] = time();
                        $pay_data['pay_status'] = 1;
                        $pay_data['paymoney'] = $tourpayment['payment'];
                        M('ToursPayment')->where(array('order_id' => $id))->save($pay_data);
                }

                $this->sysAdd("完成了订单,订单ID=" . $id);
                $this->delSuccess('操作成功');
        }

        public function order_over() {
                $id = I('get.orderid');
                $uid = get_member_id();
                M('ToursOrder')->where(array('order_id' => $id, 'mid' => $uid))->setField('status', 5);
                $this->sysAdd("取消了订单,订单ID=" . $id);
                $this->delSuccess('操作成功');
        }

        /*
         * 在线支付平台
         */

        public function online_pay() {
                if (IS_POST) {
                        $account = I('post.');
                        arr2file(APP_PATH, 'payment.php', $account);
                        $this->sysAdd("执行了【收款平台账号设置】操作");
                        $this->delSuccess("操作成功");
                } else {
                        return $this->fetch();
                }
        }

        /*
         * 收款账户
         */

        public function account() {
                $map = $this->_search("Pay");
                $this->_list(D("Pay"), $map, 'id', false, true);
                return $this->fetch();
        }

        /*
         * 收款账户添加
         */

        public function account_add($id = '') {
                if (IS_POST) {
                        if ($id) {
                                $this->_update("Pay", false, false);
                        } else {
                                $this->_insert("Pay", false, false);
                        }
                } else {
                        if ($id) {
                                $nc = M("Pay")->field(true)->find($id);
                                $this->assign('pay', $nc);
                        } else {
                                $nc['status'] = 0;
                                $nc['type'] = 0;
                                $nc['cid'] = '';
                                $this->assign('pay', $nc);
                        }
                        $bank = M('PayCate')->where(array('status' => '0'))->field('id,name')->select();
                        $this->assign('bank', $bank);
                        return $this->fetch();
                }
        }

        /*
         * 收款账号删除
         */

        public function account_del($id = '') {
                $this->_delete("Pay", $id);
        }

        //手机询单
        public function mobile_xun() {
                $map = $this->_search("MobileOrder");
                $model = D("MobileOrder");
                if (!empty($model)) {
                        $this->_list($model, $map, 'm_id', false, true);
                }
                return $this->fetch();
        }

        public function mobile_xun_detail() {
                if (IS_POST) {
                        $order = D("MobileOrder");
                        if ($order->create()) {
                                $data['result'] = I('post.result');
                                $data['status'] = I('post.status');
                                $data['mid'] = I('post.mid');
                                $order->where(array('m_id' => I("post.m_id")))->save($data);
                                return $this->iSucess("操作成功！");
                        } else {
                                return $this->iError("参数错误");
                        }
                } else {
                        $id = I("get.m_id");
                        $order = D("MobileOrder");
                        $mid = get_member_mname();
                        $this->assign('mid', $mid);
                        $ret = $order->relation(true)->field(true)->find($id);
                        if ($ret !== false) {
                                $this->assign('info', $ret);
                                return $this->fetch();
                        } else {
                                return $this->iError("数据库操作失败" . $order->getLastSql());
                        }
                }
        }

        /*
         * 在线咨询管理
         */

        public function zixun() {
                $map = $this->_search("ToursCounsel");
                if (I('content'))
                        $map['content'] = array('like', "%" . I('content') . "%");
                $model = D("ToursCounsel");
                if (!empty($model)) {
                        $this->_list($model, $map, 'create_time', false, true);
                }
                return $this->fetch();
        }

        /*
         * 在线咨询管理回复
         */

        public function zixun_reply() {
                if (IS_POST) {
                        $id = I("post.id");
                        $reply = I("post.reply");
                        $check = D("ToursCounsel")->where(array('id' => $id))->setField("reply", $reply);
                        if ($check) {
                                $this->sysAdd("回复客户咨询,ID=" . $id);
                        }
                        $this->iSucess('操作成功');
                } else {
                        $id = I("get.id");
                        $counsel = M("ToursCounsel")->field(true)->find($id);
                        $this->assign("counsel", $counsel);
                        return $this->fetch();
                }
        }

        /*
         * 在线咨询管理删除
         */

        public function zixun_del($id = '') {
                $this->_delete("ToursCounsel", $id);
        }

        /* 评论
         * 
         */

        public function comment() {
                $map = $this->_search("Comment");
                if (I('content'))
                        $map['content'] = array('like', "%" . I('content') . "%");
                $model = D("Comment");
                if (!empty($model)) {
                        $this->_list($model, $map, 'update_time', false, true);
                }
                return $this->fetch();
        }

        /*
         * 新增
         */

        public function comment_add($id = '') {
                if (IS_POST) {
                        $zhusu = I('post.zhusu');
                        $kefu = I('post.kefu');
                        $jiaotong = I('post.jiaotong');
                        $yongcan = I('post.yongcan');
                        $daoyou = I('post.daoyou');
                        $xingcheng = I('post.xingcheng');
                        $_POST['manyi'] = round((($zhusu + $kefu + $jiaotong + $yongcan + $daoyou + $xingcheng) / 30) * 100, 1);
                        if ($id) {
                                $this->_update("Comment", false, true);
                        } else {
                                $this->_insert("Comment", false, true);
                        }
                } else {
                        if ($id) {
                                $nc = M("Comment")->field(true)->find($id);
                                $this->assign('tours_comment', $nc);
                        } else {
                                $nc['status'] = 0;
                                $nc['type'] = 1;
                                $this->assign('tours_comment', $nc);
                        }
                        return $this->fetch();
                }
        }

        /*
         * 计入总分
         */

        public function comment_score($id = '') {
                $model = M('Tours');
                if (!empty($model)) {
                        $pk = $model->getPk();
                        if (isset($id)) {
                                $comment = M('Comment')->where(array('id' => $id, 'status' => 1,'jifen'=>'0'))->find();
                                if (!empty($comment)) {                                        
                                        $tours_id = $comment['tours_id'];
                                        $count = M('Comment')->where(array('tours_id' => $tours_id, 'status' => 1,'jifen'=>1))->count();
                                        $zhusu = $comment['zhusu'];
                                        $kefu = $comment['kefu'];
                                        $jiaotong = $comment['jiaotong'];
                                        $yongcan = $comment['yongcan'];
                                        $daoyou = $comment['daoyou'];
                                        $xingcheng = $comment['xingcheng'];
                                        $manyi = $comment['manyi'];
                                        $tours = M('Tours')->where(array('id' => $tours_id, 'ischecked' => 1))->find();
                                        
                                        if (!empty($tours)) {
                                                $data=array();
                                                $data1['jifen'] = 1;
                                                $data['score_jiaotong'] = round((($tours['score_jiaotong']*($count)+$jiaotong)/($count+1)),2);
                                                $data['score_canyin'] =  round((($tours['score_canyin']*($count)+$yongcan)/($count+1)),2);
                                                $data['score_chuyou'] =  round((($tours['score_chuyou']*($count)+$xingcheng)/($count+1)),2);
                                                $data['score_kefu'] =  round((($tours['score_kefu']*($count)+$kefu)/($count+1)),2);
                                                $data['score_daoyou'] =  round((($tours['score_daoyou']*($count)+$daoyou)/($count+1)),2);
                                                $data['score_zhusu'] =  round((($tours['score_zhusu']*($count)+$zhusu)/($count+1)),2);
                                                $data['manyi'] =  round((($tours['manyi']*($count)+$manyi)/($count+1)),1);
                                                M('Tours')->where(array('id' => $tours_id, 'ischecked' => 1))->save($data);
                                                M('Comment')->where(array('id' => $id))->save($data1);
                                                return $this->delSuccess('已计入总分');
                                                
                                        } else {
                                                return $this->iError('线路不存在或并未显示');
                                        }
                                } else {
                                        return $this->iError('未审核过的评论不能计入总分');
                                }
                        } else {
                                return $this->iError('非法操作');
                        }
                }
        }

        /*
         * 删除
         */

        public function comment_del($id = '') {
                $this->_delete("Comment", $id);
        }

}
